<!--  -->
<template>
  <div class="image-list-container">
    <div class="action-head">
      <el-radio-group v-model="collect"
                      @change="onChangeImage"
                      size="mini">
        <el-radio-button :label="false">全部</el-radio-button>
        <el-radio-button :label="true">收藏</el-radio-button>
      </el-radio-group>
      <el-button size="mini"
                 type="success"
                 @click="dialogUploadVisible=true"
                 v-if="isShowUpload">上传素材</el-button>
    </div>
    <!-- 素材列表 -->
    <el-row :gutter="20"
            class="image-list">
      <el-col :xs="12"
              :sm="6"
              :md="4"
              v-for="(item,index) in images"
              :key="index"
              class="image-item"
              @click.native="selected=index">
        <el-image style="height: 100px"
                  :src="item.url"
                  fit="cover"></el-image>
        <div class="selected"
             v-if="isShowSelected && selected===index"></div>
      </el-col>
    </el-row>
    <!-- 数据分页 -->
    <el-dialog title="上传素材"
               :visible.sync="dialogUploadVisible"
               :append-to-body="true">
      <el-upload class="upload-demo"
                 drag
                 action="http://api-toutiao-web.itheima.net/mp/v1_0/user/images"
                 :headers="uploadHeaders"
                 name="image"
                 :on-success="onUploadSuccess"
                 :show-file-list="false"
                 multiple>
        <!-- 组件内部发请求，不是axios要单独配置 -->
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip"
             slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import { getImages } from '@/api/images'
export default {
  name: '',
  components: {},
  props: {
    isShowUpload: {
      type: Boolean,
      default: true
    },
    isShowSelected: {
      type: Boolean,
      default: true
    }
  },
  data () {
    const user = JSON.parse(window.localStorage.getItem('user'))
    return {
      collect: false,
      images: [],
      dialogUploadVisible: false,
      uploadHeaders: {
        Authorization: `Bearer ${user.token}`
      }, // 单独配置上传图片请求
      selected: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadImages(false)
  },
  mounted () { },
  methods: {
    loadImages (collect = false) {
      getImages({ collect }).then(res => {
        this.images = res.data.data.results
      })
    },
    onChangeImage (value) {
      this.loadImages(value)
    },
    // 请求成功关闭对话框,更新素材列表
    onUploadSuccess () {
      this.dialogUploadVisible = false
      this.loadImages(false)
    }
  }
}
</script>

<style lang='less' scoped>
.action-head {
  display: flex;
  justify-content: space-between;
}
.image-list {
  margin: 20px auto;
}
.image-item {
  position: relative;
}
.selected {
  background: url(./selected.png) no-repeat;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  background-size: cover;
  opacity: 0.8;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
